package com.leyou.item.web;




import com.leyou.common.annotation.EnableExceptionAnnotation;
import com.leyou.common.dto.SpecGroupDTO;
import com.leyou.common.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * specGroup管理
 */
@RestController
@RequestMapping("/spec")
@EnableExceptionAnnotation
public class SpecGroupController {


    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;
    /**
     *  根据id查询specGroup数据
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupById(@RequestParam("id") long id){

        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(
                        specGroupService.query().eq("category_id",id).list()));
    }

    /**
     * 根据条件查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){

      return ResponseEntity.ok(specParamService.queryParamList(categoryId,groupId,searching));
    }

    /**
     *   根据分类id查询规格组及组内参数
     * @param id
     * @return List数据
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") long id){


        return ResponseEntity.ok( specGroupService.querySpecListById(id));
    }

    /**
     *新增规格租
     * @param specGroupDTO
     * @return 无
     */
    @PostMapping("/group")
    public ResponseEntity<Void>  saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){

        specGroupService.save(specGroupDTO.toEntity(SpecGroup.class));

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
        public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
            specParamService.save(specParamDTO.toEntity(SpecParam.class));

        return ResponseEntity.status(HttpStatus.CREATED).build();
        }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
